package com.zhangkeyou.map.mapweb.handler;

import com.alibaba.fastjson.JSON;
import com.zhangkeyou.map.mapweb.domain.entity.Status;
import com.zhangkeyou.map.mapweb.util.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: 张克友
 * @create: 2023-09-01 19:20
 * @description: security授权失败，访问该对象方法
 **/
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        Status result = new Status(HttpStatus.FORBIDDEN.value(),null,"未授权");
        String resultStr = JSON.toJSONString(result);
        //处理异常
        WebUtils.renderString(httpServletResponse,resultStr);
    }
}
